<?php
include_once("model/download.class.php");
include_once("model/product.class.php");
include_once("controller/JKUpload.class.php");
require_once("includes/util.php");
include_once("controller/Paging.class.php");
class downloadman 
{
	var $db;
 	var $smarty;
 	
    function downloadman($db, $smarty)
	{
		//store pointer to db onject
		$this->db = $db;
		$this->smarty = $smarty;
	}
	
	function addDownload()
	{
		$msg = download::validateDownload();
		if($msg == ""){
			if($_FILES['file']['name'] != NULL && $_FILES['file']['name'] != "") {
				$upload = new JKUpload();
				$image = $_FILES['file']['name'];
				$path = pathinfo($image);
				$image_type = strtolower($path['extension']);
				$namefile = explode(".".$image_type,$_FILES['file']['name']);
				$imagename = $upload->upload_files_txt($_FILES['file']['tmp_name'], $namefile['0'], $image_type);
			}
			$arr = array (
					'download_name' => $_POST['download_name'],
					'file'=>$imagename,
					'product_id'=>$_POST['product_id'],
					'where_id'=>$_POST['where_id']
					);
			$id = download::saveDownload($this->db, $arr);
			if($id >=0 ){
				$msg = "File has been added.";
				//Merge POST information to return back client and show at client side
			}
			setcookie("message", $msg,time()+5);
			header("Location: ./?mod=downloadman") ;
		}else{
			setcookie("message", $msg,time()+5);
			header("Location: ./?mod=downloadman&sub1=adddownload") ;	
		}
		return;
	}

	
	function editDownload(){
		if(isset( $_POST['download_id'])){
			$msgId = 2;
			$msg = download::validateDownload();
			if($msg == "")
			{

				$download = download::getDownloadById($this->db, $_POST['download_id']);
				if($_FILES['file']['name'] != NULL && $_FILES['file']['name'] != "") {
					$upload = new JKUpload();
					$image = $_FILES['file']['name'];
					$path = pathinfo($image);
					$image_type = strtolower($path['extension']);
					$namefile = explode(".".$image_type,$_FILES['file']['name']);
					$imagename = $upload->upload_files_txt($_FILES['file']['tmp_name'], $namefile['0'], $image_type);
					@unlink("../".UPLOAD_DOWNLOAD.$download->file);
				}else{
					$imagename = $download->file;
				}
				$arr = array (
					'download_id' => $_POST['download_id'],
					'download_name' => $_POST['download_name'],
					'file'=>$imagename,
					'product_id'=>$_POST['product_id'],
					'where_id'=>$_POST['where_id']
					);
				$res = download::updateDownload($this->db, $arr);

				$msgId = 1;
				setcookie("message", $msg,time()+5);
				header("Location: ./?mod=downloadman") ;
			}else{
				setcookie("message", $msg,time()+5);
				header("Location: ./?mod=downloadman&sub1=editdownload&id=".$_POST['download_id']) ;	
			}
		}	
	}
	
	function deleteDownload(){
		$arr = array("code" => -1, "message" => _("Could not delete download."));
				
				$res = partner::deletePartner($this->db, $_POST['id']);
				if($res)
				{
					$arr = array("code" => 0, 
					"message" => sprintf(_("Download with id %s has been deleted."), 
											'<b>' . $_POST['id'] . '</b>'));
					@unlink("../".UPLOAD_DOWNLOAD.$_POST['file']);
				}
				
				echo array2json($arr);
				return;
	}
	
	function process()
	{
		if(isset($_POST['act']))
		{
			$act = $_POST['act'];
			
			if($act == "adddownload")
				$this->addDownload();
			else if($act == "deldownload")
				$this->deleteDownload();
			else if($act == "editdownload")
				$this->editDownload();
		}else{
		
			if(isset($_GET['sub1']) && $_GET['sub1'] == 'editdownload')
			{
				$download = download::getDownloadById($this->db, $_GET['id']);
				$this->smarty->assign('DOWNLOAD', $download);
			}	
			$products=product::getAllProducts($this->db,$_SESSION["LC_ALL"]);
			$this->smarty->assign("PRODUCTS", $products); 
			$this->smarty->assign("PATH", "../".UPLOAD_DOWNLOAD); 
			
			$count=download::countgetAllDownload($this->db,$_SESSION["LC_ALL"],'');
 			$download['data']=download::getAllDownloadShare($this->db,$_SESSION["LC_ALL"],MAX_DEMO_PRODUCT_ADMIN,'list','');
			$link = "?mod=".$_GET['mod'];
			$result = Paging::do_paging($count,$download, MAX_DEMO_PRODUCT_ADMIN, $link,"list");
			
			$this->smarty->assign("ALLDOWNLOAD", $result);
			$this->smarty->display('download.tpl');
		}
		
	}
}
?>